package com.gougou.httpclient.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gougou.httpclient.entity.TbContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexService {
    @Autowired
    private HttpAPIService httpAPIService;

    @Value("${ApiRootURL}")
    private String ApiRootURL;


    /*************************
     *
     * 得到大广告位数据
     * 通过httpclent访问 广告页的数据
     *
     * @return
     * @throws Exception
     * *********************
     */

    public String queryIndexAD1() throws Exception {
        //从httpclien 访问得 jsonstr
        String  jsonStr= httpAPIService.doGet(ApiRootURL+"/contentApi/content?categoryId=33&page=1&rows=6");

        //利用fastjson反序列化
        Type type = new TypeReference<List<TbContent>>() {}.getType();
        List<TbContent> list = JSON.parseObject(jsonStr, type);

        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        try{
            // 解析json数据，封装成前端所需要的结构
            for (TbContent TbContent : list) {
                Map<String, Object> map = new LinkedHashMap<String, Object>();
                map.put("srcB", TbContent.getPic());
                map.put("height", 240);
                map.put("alt",TbContent.getTitle());
                map.put("width", 670);
                map.put("src", TbContent.getPic());
                map.put("widthB", 550);
                map.put("href", TbContent.getUrl());
                map.put("heightB", 240);

                result.add(map);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.toJSONString(result);
    }


    /*************************
     *
     * 得到右上广告位数据
     * 通过httpclent访问 广告页的数据
     *
     * @return
     * @throws Exception
     * *********************
     */

    public String queryIndexAD2() throws Exception {
        //从httpclien 访问得 jsonstr
        String  jsonStr= httpAPIService.doGet(ApiRootURL+"/contentApi/content?categoryId=34&page=1&rows=1");

        //利用fastjson反序列化
        Type type = new TypeReference<List<TbContent>>() {}.getType();
        List<TbContent> list = JSON.parseObject(jsonStr, type);

        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        try{
            // 解析json数据，封装成前端所需要的结构
            for (TbContent TbContent : list) {
                Map<String, Object> map = new LinkedHashMap<String, Object>();
                map.put("width", 310);
                map.put("height", 70);
                map.put("src", TbContent.getPic());
                map.put("href", TbContent.getUrl());
                map.put("alt", null);
                map.put("widthB", 210);
                map.put("heightB", 70);
                map.put("srcB", TbContent.getPic2());

                result.add(map);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.toJSONString(result);
    }

    /*************************
     *
     * 得到分类楼层广告
     * 通过httpclent访问 广告页的数据
     *
     * 传入5个 楼层id
     *
     * @return
     * @throws Exception
     * *********************
     */
    public String queryIndexAD3(ArrayList<Integer> categoryIds) throws Exception {

        ArrayList<Integer> default_categoryIds=new ArrayList<Integer>();
        default_categoryIds.add(61);
        default_categoryIds.add(62);
        default_categoryIds.add(63);
        default_categoryIds.add(64);
        default_categoryIds.add(65);


        return JSON.toJSONString(dogetAD3(default_categoryIds));
    }


    private Map dogetAD3(ArrayList<Integer> categoryIds) throws Exception {
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();


        for (int i = 0; i <5 ; i++) {
            //从httpclien 访问得 jsonstr
            String  jsonStr= httpAPIService.doGet(ApiRootURL+"/contentApi/content?categoryId="+categoryIds.get(i)+"&page=1&rows=10");

            //利用fastjson反序列化
            Type type = new TypeReference<List<TbContent>>(){}.getType();
            List<TbContent> list = JSON.parseObject(jsonStr, type);

            Map<String,Map<String, Object>> result = new LinkedHashMap<String,Map<String, Object>>();
            try{
                // 解析json数据，封装成前端所需要的结构
                Integer count=0;
                for (TbContent content : list) {
                    Map<String, Object> map = new LinkedHashMap<String, Object>();
                    map.put("d", content.getPic());
                    map.put("e", 0);
                    map.put("c", content.getSubTitle());
                    map.put("a", content.getUrl());
                    map.put("b", content.getTitle());
                    map.put("t",1);
                    count++;
                    result.put(count.toString(),map);
                }
                resultMap.put(categoryIds.get(i).toString(),result);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return resultMap;

    }


}
